﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BbsbWebService.Classes;
using BbsbWebService.Enums;

namespace BbsbWebService
{
    /// <summary>
    /// Interaction logic for AddGroupWindow.xaml
    /// </summary>
    public partial class AddGroupWindow : Window
    {
        List<ControllableItem> allItemsList;
        List<ControllableItem> memberItemsList;

        public AddGroupWindow(Group group)
        {
            InitializeComponent();
            Group = group;
            Init();
            nameTxt.Text = Group.Name;
            commandTxt.Text = Group.Command;
            groupTypeCmb.SelectedIndex = (int)Group.Type;
        }

        public AddGroupWindow()
        {
            InitializeComponent();
            Group = new Group();
            Group.Id = Guid.NewGuid();
            Init();
            groupTypeCmb.SelectedIndex = 0;
        }

        private void Init()
        {
            groupTypeCmb.ItemsSource = Enum.GetNames(typeof(GroupType)).Skip(1);
            allItemsList = Group.NotGroupItems.ToList();
            memberItemsList = Group.GroupItems.ToList();
            Refresh();
        }

        public Group Group { get; set; }

        private void saveBtn_Click(object sender, RoutedEventArgs e)
        {
            Group.Name = nameTxt.Text;
            if (groupTypeCmb.SelectedItem != null)
            {
                Group.Type = groupTypeCmb.SelectedItem.ToString().ToEnum<GroupType>();
            }
            Group.Command = commandTxt.Text;
            List<ControllableItem> items = memberItems.ItemsSource as List<ControllableItem>;
            if (items != null)
            {
                Group.EditedGroupItems = items;
            }

            var state = Group.ValidationState;
            if (state.IsValid)
            {
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show(state.ToString());
            }
        }

        private void joinGroup_Click(object sender, RoutedEventArgs e)
        {
            if (allItems.SelectedItems != null && allItems.SelectedItems.Count > 0)
            {
                foreach (var item in allItems.SelectedItems)
                {
                    ControllableItem bob = item as ControllableItem;
                    if (bob != null)
                    {
                        memberItemsList.Add(bob);
                        allItemsList.Remove(bob);
                    }
                }
                Refresh();
            }
        }

        private void removeGroup_Click(object sender, RoutedEventArgs e)
        {
            if (memberItems.SelectedItem != null)
            {
                ControllableItem bob = memberItems.SelectedItem as ControllableItem;
                if (bob != null)
                {
                    allItemsList.Add(bob);
                    memberItemsList.Remove(bob);
                    Refresh();
                }
            }
        }

        private void cancelBtn_Click(object sender, RoutedEventArgs e)
        {
            Group = null;
            this.Close();
        }

        private void Refresh()
        {
            allItems.ItemsSource = allItemsList;
            allItems.Items.Refresh();
            memberItems.ItemsSource = memberItemsList;
            memberItems.Items.Refresh();
        }
    }
}
